package August.Rob3;

import java.util.ArrayList;
import java.util.List;

//337. 打家劫舍 III
public class R3 {
    
}


class TreeNode {
     int val;
     TreeNode left;
     TreeNode right;
     TreeNode(int x) { val = x; }
}

class Solution {
    public int rob(TreeNode root) {
        if (root==null) return 0;
        List<Integer> ans = postOrder(root);
        return Math.max(ans.get(0), ans.get(1));
    }

    private List<Integer> postOrder(TreeNode root){
        if(root==null) return null;
        List<Integer> l = postOrder(root.left);
        List<Integer> r = postOrder(root.right);
        List<Integer> rootNode = new ArrayList<>();
        int maxC = 0;
        int maxCC = 0;
        if(l!=null){
            maxC += l.get(0);
            maxCC += l.get(1);
        }
        if(r!=null){
            maxC += r.get(0);
            maxCC += r.get(1);
        }
        maxCC+=root.val;
        rootNode.add(Math.max(maxC,maxCC));
        rootNode.add(maxC);
        return rootNode;
    }
}